/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package IO;
import java.io.*;
import java.util.ArrayList;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 *
 * @author ndnguyen
 */
public class IOFIle {
    public IOFIle()
    {    
    }
   /**
    * @return : values the value of str
    * @param str: string that we find the value
    * vd : server = xxx.yyy
    *  return xxx.yyy
    */

   
   private static String getContent(String str)
   {
       int index=str.lastIndexOf("=");
       return str.substring(index+1, str.length()).trim();
   }

   public static void ReadAndWrite(String fromFile,String toFile)
   {
        File f = new File(fromFile);
        FileReader frd;
        BufferedReader brd;
        FileWriter fw;
        BufferedWriter bw;
        try
        {
            frd = new FileReader(f);
            brd = new BufferedReader(frd);
            fw = new FileWriter(toFile);
            bw = new BufferedWriter(fw);
   //         bw.write("<?xml version=\"1.0\"?>\r\n");
            bw.write("<exam>\r\n");
            while(brd.ready())
            {
                String line = brd.readLine();
                bw.write(line + "\r\n");
            }
            bw.write("</exam>");
            bw.close();
            brd.close();
        }
        catch(Exception e)
        {
        }
   }

   /**
    * @return : Server and ServerPort 
    *   1: value of sserver
    *   2: value of server port
    * @param filename : name of file we need to read    
    */    
    public static List<String> readFileConfigure(String filename)  //OK
    {
        List<String> list = new ArrayList<String>();
        File file = new File(filename);
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;

        try {
          fis = new FileInputStream(file);
          
          // Here BufferedInputStream is added for fast reading.
          bis = new BufferedInputStream(fis);
          dis = new DataInputStream(bis);
           int i=0;
          // dis.available() returns 0 if the file does not have more lines.
          while (dis.available() != 0) {

          // this statement reads the line from the file and print it to
            // the console.
            list.add(i++, getContent(dis.readLine()));
      //      System.out.println(dis.readLine());
          }

          // dispose all the resources after using them.
          fis.close();
          bis.close();
          dis.close();

        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }

        return list;
    }
    /**
    * @return : Server and ServerPort
    *   1: value of sserver
    *   2: value of server port
    * @param filename : name of file we need to read
    */
    public static List<String> readFileStudent(String filename)  //OK
    {
        List<String> list = new ArrayList<String>();
        File file = new File(filename);
        if(file.exists()==true)
        {
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        DataInputStream dis = null;

        try {
          fis = new FileInputStream(file);

          bis = new BufferedInputStream(fis);
          dis = new DataInputStream(bis);
           int i=0;        
          while (dis.available() != 0) {
            list.add(i++,dis.readLine());
          }

          fis.close();
          bis.close();
          dis.close();

        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
        }
      
        return list;
    }
    /**
     *
     * @param idClient :  id of client ,we use this id to write to file that has name id.xml
     * @param numberOfQuestion : the order of question
     * @param answer : answer of that question .
     */
    public static void WriteOneAnswerToXML(String idClient,String numberOfQuestion,String answer) //OK
    {
        try
        {
           FileWriter fsstream = new FileWriter(idClient+".xml");
           BufferedWriter out = new BufferedWriter(fsstream);
           out.write("<question> <number>"+numberOfQuestion+"</number> <answer>"+answer+"</answer>  </question>");
           out.close();
        }
        catch(Exception e)
        {
            System.err.println("Error :"+e.getMessage());
        }
    }

      /**
     *
     * @param idClient :  id of client ,we use this id to write to file that has name id.xml
     * @param numberOfQuestion : the order of question
     * @param answer : answer of that question .
     */

    public static void WriteAnswersToFile(String idClient,List<ClientAnswer> answerList) //OK
    {
        try
        {
           FileWriter fsstream = new FileWriter(idClient+".txt");
           BufferedWriter out = new BufferedWriter(fsstream);
           int numQues = answerList.size();
           for(int i=0;i<numQues;i++)
           {
                out.write(String.valueOf(i+1));
                out.write("\r\n");
                out.write(answerList.get(i).getAnswer());
                out.write("\r\n");   
           }
          out.close();
        }
        catch(Exception e)
        {
            System.err.println("Error :"+e.getMessage());
        }
    }

    public static void WriteStudentToFile(String name,String id,String dob) //OK
    {
        try
        {
          
           FileWriter fsstream = new FileWriter("D:\\studentInfo.txt");
           BufferedWriter out = new BufferedWriter(fsstream);         
            out.write(name);
            out.write("\r\n");
            out.write(id);
            out.write("\r\n");
            out.write(dob);
            out.write("\r\n");
          out.close();
        }
        catch(Exception e)
        {
            System.err.println("Error :"+e.getMessage());
        }
    }

     public static void main(String[] args)
     {
        WriteOneAnswerToXML("2", "1", "a");
        List<String> ls  = new ArrayList<String>();
        ls = readFileConfigure("configure.txt");
        for(int i=0;i<ls.size();i++)
        {
            System.out.println(" hic hic  ");
            System.out.println(ls.get(i));
        }

     }
  }


